<!DOCTYPE html>
<html mip>
<head>
    <meta name='viewport' content='width=device-width, initial-scale=1'/>
    <meta charset="utf-8"/>
    <link rel="shortcut icon" href="#"/>
    <title>Netty Im Web Client</title>
</head>
<body>
<div>
    <h4>Netty Im Web Client</h4>

    <form onsubmit="return false">
        <textarea name="message" style="width: 400px;height: 200px"></textarea>
        <input type="button" value="发送数据" onclick="sendMessage(this.form.message.value);">
        <input type="text" name="receiverId" value="880204" placeholder="数据接收人ID">

        <h3>服务器输出：</h3>
        <textarea id="responseText" style="width: 400px;height: 300px;"></textarea>
        <input type="button" onclick="javascript:document.getElementById('responseText').value=''" value="清空数据">
    </form>
</div>
</body>
<script type='text/javascript' src='${rc.contextPath}/plugins/jquery/jquery-2.1.4.min.js'></script>
<script type='text/javascript' src='${rc.contextPath}/plugins/cheetah/cheetah.js'></script>
<script type='text/javascript'>
    let appId = "f8df32df00pc9ds8r0g";
    let userId = "880205";

    Cheetah.Instance = new Cheetah.CheetahSDK();
    Cheetah.Instance.setMsgServerInfo("localhost", 7778);
    Cheetah.Instance.setIdleStateTime(35000, 35000);

    let userCallback = function (data, status) {
        switch (status) {
            case "connect success":
                console.log(data);
                break;
            case "connect failed":
                console.log(data);
                break;
            case "connect closed":
                console.log(data);
                break;
            case "onLoginMessage":
                console.log(data);
                break;
            case "onSingleMessage":
                messageShow(data.data);
                break;
            case "onGroupMessage":
                break;
            case "onSystemPushMessage":
                break;
            case "onIdleStateMessage":
                console.log(data);
                break;
            case "onErrorMessage":
                break;
            case "onGetOnlineNumber":
                break;
        }
    };

    Cheetah.Instance.login(appId, userId, userCallback);

    function sendMessage(value){
        let message = {
            message: value,
            receiverId: $("input[name='receiverId']").val(),
        }
        Cheetah.Instance.sendSingleMessage(message);
    }

    function messageShow(data){
        $("#responseText").text($("#responseText").text() + data.fromId + ": " + data.message + "\r\n");
    }

</script>
</html>